package base

import (
	"strings"
	"unicode"
)

var CommonUtil = &CommonUtilTool{}

type CommonUtilTool struct{}

func (*CommonUtilTool) ReplaceStr(str string, replace string, new string) string {
	strT := str
	for {
		t := strings.Replace(strT, replace, new, -1)
		if t == strT {
			break
		}
		strT = t
	}
	return strT
}

func (*CommonUtilTool) ReplaceStrs(str string, replace []string, new string) string {
	for _, r := range replace {
		str = strings.Replace(str, r, new, -1)
	}
	return str
}

func (*CommonUtilTool) TrimStr(str string, trim string) string {
	return strings.Trim(str, trim)
}

// UnderScoreToCamelCase 将下划线命名转为驼峰命名
// 例如：ab_test -> AbTest, user_name -> UserName
func (*CommonUtilTool) UnderScoreToCamelCase(s string) string {
	// 分割下划线
	parts := strings.Split(s, "_")

	// 处理每个部分
	for i := 0; i < len(parts); i++ {
		if len(parts[i]) == 0 {
			continue
		}

		// 将首字母大写
		runes := []rune(parts[i])
		runes[0] = unicode.ToUpper(runes[0])
		parts[i] = string(runes)
	}

	// 合并所有部分
	return strings.Join(parts, "")
}
